package com.ytkj.digitalConstruction.controller.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.ytkj.digitalConstruction.ReturnCode;
import com.ytkj.digitalConstruction.entity.user.User;
import com.ytkj.digitalConstruction.entity.user.UserGroupUser;
import com.ytkj.digitalConstruction.httpbean.PageResult;
import com.ytkj.digitalConstruction.httpbean.user.req.ReqUserGroup;
import com.ytkj.digitalConstruction.httpbean.user.req.UserListByUserGroupId;
import com.ytkj.digitalConstruction.httpbean.user.resbean.ReturnUserGroup;
import com.ytkj.digitalConstruction.service.user.UserGroupService;

/**
 * 描述: 用户组管理
 *
 * @author wxm
 * @create 2017-12-15 9:25
 */
@RestController
@RequestMapping("/userGroup")
public class UserGroupController {
	private final static Logger log = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserGroupService userGroupService;

	/**
	 * 根据人员id获取用所有用户组信息
	 * 获取所有用户组信息
	 *
	 * @return
	 */
	@GetMapping("/getUserGroups")
	public ReturnCode getUserGroups(HttpServletRequest request) {
		List<ReturnUserGroup> list = userGroupService.getUserGroups(request);
		if (list == null) {
			log.error("获取用户组信息失败");
			return new ReturnCode.Builder().Success(false).Msg("获取用户组信息失败").build();
		}
		log.info("获取用户组信息开始");
		return new ReturnCode.Builder().Success(true).Msg("获取用户组信息成功").Object(list).build();
	}

	/**
	 * 
	 * @ClassName: UserCroupController
	 * @Description:根据用户组id查询用户组信息
	 * @param userListByUserGroupId
	 * @return
	 */
	@PostMapping("/getUserListByUserGroupId")
	public ReturnCode getUserGroupId(@RequestBody @Valid UserListByUserGroupId userListByUserGroupId, BindingResult bindingResult) {
		// 判断参数是否合法
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error(msg);
			return new ReturnCode.Builder().Success(false).Msg(msg).build();
		}
		Page<User> userList = userGroupService.getUserListByUserGroupId(userListByUserGroupId);
		if (userList == null) {
			log.error("获取用户组信息失败");
			return new ReturnCode.Builder().Success(false).Msg("获取用户组信息失败").build();
		}
		log.info("获取用户组信息成功");
		PageResult<User> result = new PageResult<User>();
		result.setItemTotal(userList.getTotal());
		result.setItems(userList.getResult());
		return new ReturnCode.Builder().Success(true).Msg("获取用户组信息成功").Object(result).build();
	}

	

	/**
	 * 修改用户组信息
	 *
	 * @return
	 */
	@PostMapping("/updateUserGroups")
	public ReturnCode updateUserGroups(@RequestBody @Valid ReqUserGroup reqUserGroup, HttpServletRequest request) {
		String  userGroupName = reqUserGroup.getUserGroupName();
		if(userGroupName == null) {
			return new ReturnCode.Builder().Success(false).Msg("修改用户组名称不能为空，请重新填写").build();
		}
		boolean flag = userGroupService.updateUserGroup(reqUserGroup,request);
		if (flag == false) {
			log.error("修改用户组信息失败");
			return new ReturnCode.Builder().Success(false).Msg("修改用户组信息失败").build();
		}
		log.info("修改用户组信开始");
		return new ReturnCode.Builder().Success(true).Msg("修改用户组信息成功").Object(reqUserGroup).build();
	}

	/**
	 * 新增用户组信息
	 *
	 * @return
	 */
	@PostMapping("/insertUserGroups")
	public ReturnCode insertUserGroup(@RequestBody @Valid ReqUserGroup reqUserGroup, HttpServletRequest request) {
		boolean flag = userGroupService.insertUserGroup(reqUserGroup,request);
		if (flag == false) {
			log.error("新增用户组信息失败");
			return new ReturnCode.Builder().Success(false).Msg("新增用户组信息失败").build();
		}
		log.info("新增用户组信息开始");
		if(reqUserGroup.getUserGroupDesc() == null) {
			reqUserGroup.setUserGroupDesc("");
		}
		return new ReturnCode.Builder().Success(true).Msg("新增用户组信息成功").Object(reqUserGroup).build();
	}

	/**
	 * 删除用户组信息
	 *
	 * @return
	 */
	@GetMapping("/deleteUserGroup/{userGroupId}")
	public ReturnCode deleteUserGroup(@PathVariable("userGroupId") Integer userGroupId) {
		// 判断参数是否合法
		if (userGroupId == null || userGroupId == 0) {
			log.error("传入参数不合法");
			return new ReturnCode.Builder().Success(false).Msg("传入参数不合法").build();
		}
		// 判断删除是否成功
		boolean result = userGroupService.deleteUserGroup(userGroupId);
		if (result == false) {
			log.error("用户组有绑定用户，不可删除！");
			return new ReturnCode.Builder().Success(false).Msg("用户组有绑定用户，不可删除！").build();
		}
		log.info("开始删除用户组");
		return new ReturnCode.Builder().Success(true).Msg("删除用户组信息成功").Object(result).build();
	}

	/**
	 * 删除用户组下面的人员
	 * 
	 * @param userGroupUser
	 * @param bindingResult
	 * @return
	 */
	@PostMapping("/deleteUser")
	public ReturnCode deleteUser(@RequestBody @Valid UserGroupUser userGroupUser, BindingResult bindingResult) {
		// 判断参数是否合法
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error(msg);
			return new ReturnCode.Builder().Success(false).Msg(msg).build();
		}

		Integer result = userGroupService.deleteUserGroupUser(userGroupUser);
		if (result == null || result == 0) {
			log.error("删除用户组下面的人员失败");
			return new ReturnCode.Builder().Success(false).Msg("删除用户组下面的人员失败").build();
		}
		log.info("开始删除用户组下面的人员");
		return new ReturnCode.Builder().Success(true).Msg("删除用户组下面的人员成功").build();
	}

	/**
	 * 部门下新增人员
	 * 
	 * @param reqUserGroup
	 * @param bindingResult
	 * @return
	 */
	@PostMapping("/addUser")
	public ReturnCode addUser(@RequestBody @Valid ReqUserGroup reqUserGroup, BindingResult bindingResult) {
		// 判断参数是否合法
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			log.error(msg);
			return new ReturnCode.Builder().Success(false).Msg(msg).build();
		}
		boolean result = userGroupService.insertUserGroupUser(reqUserGroup);
		if (result == false) {
			log.error("增加用户组下面的人员失败");
			return new ReturnCode.Builder().Success(false).Msg("增加用户组下面的人员失败").build();
		}
		log.info("增加用户组下面的人员成功");
		return new ReturnCode.Builder().Success(true).Msg("增加用户组下面的人员成功").build();
	}

	
}
